package com.tvsinfo.dict;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class XMl {


    public static void main(String[] args) {
        XMl xMl = new XMl();
        xMl.rexml();

    }


    public void rexml() {

        List<wordbook> wordbooks = new ArrayList<>();

        try {
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File("D:\\20191115.xml"));
            Element root = document.getRootElement();


            for (Iterator<Element> it = root.elementIterator("item"); it.hasNext(); ) {
                Element foo = it.next();
                System.out.println("word:" + foo.element("word").getData());
                System.out.println("trans:" + foo.element("trans").getData());
                System.out.println("phonetic:" + foo.element("phonetic").getData());
                System.out.println("tags:" + foo.element("tags").getData());
                System.out.println("progress:" + foo.element("progress").getData());
                System.out.println("-----------------------------------------------");
                wordbook wordbook = new wordbook();
                wordbook.setWord(foo.element("word").getData().toString());
                wordbook.setTags(foo.element("tags").getData().toString());
                wordbook.setTrans(foo.element("trans").getData().toString());
                wordbooks.add(wordbook);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        Map<String, List<wordbook>> collect = wordbooks.stream().collect(Collectors.toMap(wordbook::getTags, this::getList, (list, list2) -> {
            list.addAll(list2);
            return list;
        }));

        collect.forEach((s, wordbooks1) -> {
            Document document = DocumentHelper.createDocument();
            Element root = document.addElement("wordbook");

            wordbooks1.forEach(wordbook -> {
                Element author1 = root.addElement("item");
                author1.addElement("word").addText(wordbook.getWord());
                author1.addElement("trans").addText(wordbook.getTrans());
            });

            try {
                FileWriter out = new FileWriter("D:\\data\\dict\\" + s + ".xml");
                OutputFormat format = OutputFormat.createPrettyPrint();
                XMLWriter writer = new XMLWriter(out, format);
                writer.write(document);
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        });


        System.out.println(collect);
    }


    public List getList(wordbook wordbook) {
        List list = new ArrayList();
        list.add(wordbook);
        return list;
    }

    class wordbook {

        private String word;
        private String trans;
        private String tags;

        public String getWord() {
            return word;
        }

        public void setWord(String word) {
            this.word = word;
        }

        public String getTrans() {
            return trans;
        }

        public void setTrans(String trans) {
            this.trans = trans;
        }

        public String getTags() {
            return tags.equals("") ? "Java" : tags;
        }

        public void setTags(String tags) {
            this.tags = tags;
        }
    }

}
